#bilibili排名算法 v2.4(2011.4.20)
#针对『热门新番』的特殊规则
#此规则仅在周点击>20000时生效
#评论分 = 评论数×(50,50×(50000/(点击数+评论数×50))).min
#收藏分 = 收藏数×10，
class Results::R24 < Results::R23
  @is_hot_bangumi =nil

  def is_hot_bangumi?
    if @is_hot_bangumi.nil?
      if work.ac_type_id == 25 && clicks > 20000
        bw = BangumiWork.first :include => :bangumi, :conditions => ["work_id = ? and bangumis.status = 1", work_id]
        self[:hot_bangumi] = true if bw
        @is_hot_bangumi = (bw ? true : false)
      else
        @is_hot_bangumi = false
      end
    else
      @is_hot_bangumi
    end
  end

  def self.get_comment_revised clicks, comments, stows, ops={}
    if ops[:hot_bangumi]
      if comments <= 0
        E_C.to_f
      else
        [format("%.2f", E_C.to_f * (clicks + 20000) / (clicks.to_f + comments.to_f * E_C)).to_f, E_C.to_f].min
      end
    else
      if comments <= 0
        E_C.to_f
      else
        [format("%.2f", E_C * (stows * E_S) / (clicks.to_f + comments.to_f * E_C)).to_f, E_C.to_f].min
      end
    end
  end

  def comment_revised
    if is_hot_bangumi?
      self.class.get_comment_revised clicks, comments, stows, :hot_bangumi => true
    else
      self.class.get_comment_revised clicks, comments, stows
    end
  end

  def self.get_stow_revised stows, ops={}
    if ops[:hot_bangumi]
      10
    else
      if stows <= 300
        E_S
      else
        fixed = (E_S*300 + 20*(stows - 300))/stows.to_f
        format("%.2f", fixed).to_f
      end
    end
  end

  def stow_revised
    if is_hot_bangumi?
      self.class.get_stow_revised stows, :hot_bangumi => true
    else
      self.class.get_stow_revised stows
    end
  end
  
end
